package com.one.rope.mvp.web.business.service.impl;

import com.one.rope.mvp.web.business.entity.ComArea;
import com.one.rope.mvp.web.business.service.IComAreaService;
import com.one.rope.mvp.web.business.service.ICommonDataService;
import com.one.rope.mvp.web.common.cache.GlobalCacheDataObject;
import com.one.rope.mvp.basecore.bean.ResponseObject;
import com.one.rope.mvp.web.common.dto.common.AreaInfoVO;
import com.one.rope.mvp.web.config.CommonConfig;
import com.one.rope.mvp.web.config.GlobalConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Weijian.Liu
 * @desc 通用数据服务实现类
 * @date 2020/2/23 3:06
 */
@Service
public class CommonDataServiceImpl implements ICommonDataService {

  @Autowired
  IComAreaService comAreaService;

  @Autowired
  CommonConfig commonConfig;

  @Override
  public ResponseObject projectSpotTagList() {
    return new ResponseObject(GlobalConfig.PROJECT_SPOT_CONFIG_LIST);
  }

  @Override
  public ResponseObject memberSkilledTageList() {
    return new ResponseObject(GlobalConfig.MEMBER_SKILLED_CONFIG_LIST);
  }

  @Override
  public ResponseObject companyLayTagList() {
    return new ResponseObject(GlobalConfig.CONPANY_LAY_CONFIG_LIST);
  }

  @Override
  public ResponseObject personalLayTagList() {
    return new ResponseObject(GlobalConfig.PERSONAL_LAY_CONFIG_LIST);
  }

  @Override
  public List<AreaInfoVO> selectAreaList() {
    List<AreaInfoVO> areaInfoList = new ArrayList<>();
    List<ComArea> comAreaList = comAreaService.list();
    if (comAreaList.size() > 0) {
      comAreaList.stream().forEach(item -> {
        AreaInfoVO areaInfo = new AreaInfoVO();
        areaInfo.setAreaCode(item.getTxtAreaId());
        areaInfo.setAreaName(item.getTxtAreaName());
        areaInfo.setCodLevel(item.getCodLevel());
        areaInfo.setTxtParentId(item.getTxtParentId());
        areaInfoList.add(areaInfo);
      });
    }
    return areaInfoList;
  }

  @Override
  public ResponseObject areaInfoList() {
    // 从缓存获取数据
    List<AreaInfoVO> areaInfoList = GlobalCacheDataObject.getInstance().getAllAreaDataList();
    // 组装结构化数据
    // 区域数据按级别分组
    Map<Integer, List<AreaInfoVO>> levelgroupMap = areaInfoList.stream()
        .collect(Collectors.groupingBy(AreaInfoVO::getCodLevel));
    // 获取所有的省级列表
    List<AreaInfoVO> provinceList = levelgroupMap.get(1);
    List<AreaInfoVO> cityList = levelgroupMap.get(2);
    List<AreaInfoVO> districtList = levelgroupMap.get(3);
    provinceList.stream().forEach(province -> {

      // 获取该省级下面的市级列表
      List<AreaInfoVO> cityListByProvince = cityList.stream()
          .filter(city -> city.getTxtParentId().equals(province.getAreaCode()))
          .collect(Collectors.toList());
      cityListByProvince.stream().forEach(city -> {
        // 获取该市级下面的区级列表
        List<AreaInfoVO> districtListByCity = districtList.stream()
            .filter(district -> district.getTxtParentId().equals(city.getAreaCode()))
            .collect(Collectors.toList());
        city.setChildList(districtListByCity);
      });

      province.setChildList(cityListByProvince);
    });

    return new ResponseObject(provinceList);
  }

}
